# Azure API人脸识别
import requests
import json

subscription_key = "5e123cd0b7e04f9289a01350df3027f9" #填入秘钥
assert subscription_key

face_api_url = 'https://apifacetest.cognitiveservices.azure.cn/face/v1.0/detect' #face后面的/v1.0/detect是固定的

# 本地图片上传转URL网站
image_url = 'https://i.loli.net/2020/10/26/thFgq3LwnBGpQuV.jpg' 

headers = {'Ocp-Apim-Subscription-Key': subscription_key}

params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,smile,glasses,hair',
}

response = requests.post(face_api_url, params=params,
                         headers=headers, json={"url": image_url})
# json.dumps 将json--->bytes
json.dumps(response.json()) # response.content/ response.text/ response.json() 三种请求方法，前两种以字符串的方式呈现，不好处理。json列表以逗号隔开

# '[{"faceId": "5ab86da9-3267-43b5-84a4-5dad06b8b8a7", "faceRectangle": {"top": 370, "left": 787, "width": 368, "height": 368}, "faceAttributes": {"smile": 0.059, "gender": "male", "age": 18.0, "glasses": "NoGlasses", "hair": {"bald": 0.44, "invisible": false, "hairColor": [{"color": "black", "confidence": 0.99}, {"color": "brown", "confidence": 0.63}, {"color": "other", "confidence": 0.54}, {"color": "gray", "confidence": 0.44}, {"color": "blond", "confidence": 0.07}, {"color": "red", "confidence": 0.04}]}}}, {"faceId": "260bbbb9-10d3-40b8-b039-90048cb44e2b", "faceRectangle": {"top": 523, "left": 154, "width": 313, "height": 313}, "faceAttributes": {"smile": 1.0, "gender": "male", "age": 24.0, "glasses": "ReadingGlasses", "hair": {"bald": 0.0, "invisible": true, "hairColor": []}}}, {"faceId": "9dc5f6ae-8e8c-4b34-a51d-b196e54783ab", "faceRectangle": {"top": 682, "left": 491, "width": 239, "height": 239}, "faceAttributes": {"smile": 0.977, "gender": "male", "age": 25.0, "glasses": "ReadingGlasses", "hair": {"bald": 0.18, "invisible": false, "hairColor": [{"color": "black", "confidence": 0.98}, {"color": "other", "confidence": 0.66}, {"color": "brown", "confidence": 0.52}, {"color": "gray", "confidence": 0.32}, {"color": "red", "confidence": 0.13}, {"color": "blond", "confidence": 0.12}]}}}]'

# json()获取内容（results）
results = response.json()
results

# [{'faceId': '5ab86da9-3267-43b5-84a4-5dad06b8b8a7',
#   'faceRectangle': {'top': 370, 'left': 787, 'width': 368, 'height': 368},
#   'faceAttributes': {'smile': 0.059,
#    'gender': 'male',
#    'age': 18.0,
#    'glasses': 'NoGlasses',
#    'hair': {'bald': 0.44,
#     'invisible': False,
#     'hairColor': [{'color': 'black', 'confidence': 0.99},
#      {'color': 'brown', 'confidence': 0.63},
#      {'color': 'other', 'confidence': 0.54},
#      {'color': 'gray', 'confidence': 0.44},
#      {'color': 'blond', 'confidence': 0.07},
#      {'color': 'red', 'confidence': 0.04}]}}},
#  {'faceId': '260bbbb9-10d3-40b8-b039-90048cb44e2b',
#   'faceRectangle': {'top': 523, 'left': 154, 'width': 313, 'height': 313},
#   'faceAttributes': {'smile': 1.0,
#    'gender': 'male',
#    'age': 24.0,
#    'glasses': 'ReadingGlasses',
#    'hair': {'bald': 0.0, 'invisible': True, 'hairColor': []}}},
#  {'faceId': '9dc5f6ae-8e8c-4b34-a51d-b196e54783ab',
#   'faceRectangle': {'top': 682, 'left': 491, 'width': 239, 'height': 239},
#   'faceAttributes': {'smile': 0.977,
#    'gender': 'male',
#    'age': 25.0,
#    'glasses': 'ReadingGlasses',
#    'hair': {'bald': 0.18,
#     'invisible': False,
#     'hairColor': [{'color': 'black', 'confidence': 0.98},
#      {'color': 'other', 'confidence': 0.66},
#      {'color': 'brown', 'confidence': 0.52},
#      {'color': 'gray', 'confidence': 0.32},
#      {'color': 'red', 'confidence': 0.13},
#      {'color': 'blond', 'confidence': 0.12}]}}}]

# 在用数据结构查询的方式查找到：眼镜、肤色、微笑指数、头发颜色、年龄、性别这几项数据（faceAttributes中没有肤色的属性）
# 存进变量/自定义数据结构（如字典等）当中
dict01 = {}
dict01["man01-smile"]= (results[0]['faceAttributes']['smile'])
dict01["man01-gender"]= (results[0]['faceAttributes']['gender'])
dict01["man01-age"]= (results[0]['faceAttributes']['age'])
dict01["man01-glasses"]= (results[0]['faceAttributes']['glasses'])
dict01["man01-hair"]= (results[0]['faceAttributes']['hair'])
print(dict01)

# {'man01-smile': 0.059, 'man01-gender': 'male', 'man01-age': 18.0, 'man01-glasses': 'NoGlasses', 'man01-hair': {'bald': 0.44, 'invisible': False, 'hairColor': [{'color': 'black', 'confidence': 0.99}, {'color': 'brown', 'confidence': 0.63}, {'color': 'other', 'confidence': 0.54}, {'color': 'gray', 'confidence': 0.44}, {'color': 'blond', 'confidence': 0.07}, {'color': 'red', 'confidence': 0.04}]}}

dict02 = {}
dict02["man02-smile"]= (results[1]['faceAttributes']['smile'])
dict02["man02-gender"]= (results[1]['faceAttributes']['gender'])
dict02["man02-age"]= (results[1]['faceAttributes']['age'])
dict02["man02-glasses"]= (results[1]['faceAttributes']['glasses'])
dict02["man02-hair"]= (results[1]['faceAttributes']['hair'])
print(dict02)

# {'man02-smile': 1.0, 'man02-gender': 'male', 'man02-age': 24.0, 'man02-glasses': 'ReadingGlasses', 'man02-hair': {'bald': 0.0, 'invisible': True, 'hairColor': []}}

dict03 = {}
dict03["man03-smile"]= (results[2]['faceAttributes']['smile'])
dict03["man03-gender"]= (results[2]['faceAttributes']['gender'])
dict03["man03-age"]= (results[2]['faceAttributes']['age'])
dict03["man03-glasses"]= (results[2]['faceAttributes']['glasses'])
dict03["man03-hair"]= (results[2]['faceAttributes']['hair'])
print(dict03)

# {'man03-smile': 0.977, 'man03-gender': 'male', 'man03-age': 25.0, 'man03-glasses': 'ReadingGlasses', 'man03-hair': {'bald': 0.18, 'invisible': False, 'hairColor': [{'color': 'black', 'confidence': 0.98}, {'color': 'other', 'confidence': 0.66}, {'color': 'brown', 'confidence': 0.52}, {'color': 'gray', 'confidence': 0.32}, {'color': 'red', 'confidence': 0.13}, {'color': 'blond', 'confidence': 0.12}]}}
